package com.sh.suhe.utildemos.views.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.model.MainModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_out_excel.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.lang.Exception

/**
 * 生成excel 文件，使用修改过的poi
 * poi包来源 https://github.com/andruhon/android5xlsx
 */
class OutExcelActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_out_excel)
        excel_but.setOnClickListener {
            excel_but.isEnabled = false
            askPermission()
            excel_but.isEnabled = true
        }

        askPermission()
    }
    private fun createExcel(){
        try {
            exportExcel()
        }catch (e: Exception){

        }

    }
    @Throws(IOException::class)
    fun exportExcel() {
        val path = Environment.getExternalStorageDirectory().absolutePath

        val list = ArrayList<MainModel>()
        for ( i in 1..10) {
            list.add(MainModel("title"+i, "href"+i, "desc"+1))
        }

        //1.在内存中创建一个excel文件
        val hssfWorkbook = HSSFWorkbook()
        //2.创建工作簿
        val sheet = hssfWorkbook.createSheet()
        //3.创建标题行
        val titlerRow = sheet.createRow(0)
        titlerRow.createCell(0).setCellValue("title")
        titlerRow.createCell(1).setCellValue("href")
        titlerRow.createCell(2).setCellValue("desc")
//        titlerRow.createCell(3).setCellValue("邮编")
//        titlerRow.createCell(4).setCellValue("简码")
//        titlerRow.createCell(5).setCellValue("城市编码")

        //4.遍历数据,创建数据行
        for (area in list) {
            //获取最后一行的行号
            val lastRowNum = sheet.lastRowNum
            val dataRow = sheet.createRow(lastRowNum + 1)
            dataRow.createCell(0).setCellValue(area.title )
            dataRow.createCell(1).setCellValue(area.href)
            dataRow.createCell(2).setCellValue(area.desc)
//            dataRow.createCell(3).setCellValue(area.getPostcode())
//            dataRow.createCell(4).setCellValue(area.getShortcode())
//            dataRow.createCell(5).setCellValue(area.getCitycode())
        }
        //5.创建文件名
        var fileName = "区域数据统计.xls"
        //6.获取输出流对象
        val file = File(path,fileName)

        val outputStream = FileOutputStream(file)

        //10.写出文件,关闭流
        hssfWorkbook.write(outputStream)
        hssfWorkbook.close()
    }
    fun askPermission() {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.STORAGE)
                .onGranted {
                    createExcel()
                }
                .onDenied{
                    Toast.makeText(this,"您拒绝了权限，有些功能会有影响！",Toast.LENGTH_SHORT).show() //("您拒绝了权限，有些功能会有影响！")

                }
                .start()
    }
}
